<?php 
$page_title = "21 Game - Notifications";
include("header.php");
if(!isset($_SESSION['username'])){
	header("Location: index.php");
}
 ?>

<!-- page header -->
<div class="page-header">
	<div class="container">
		<h1><span style="color: red;">Notifications</span></h1>
	</div>
</div>
<!-- end page header -->
<div class="container">
<?php 
	require_once("sql/connection.php");
	require_once("utilities/functions.php");

	//Getting your user_id
	$playerid = getPlayerId($dbc, $_SESSION['username']);
	
	$query = "SELECT notification_id, title, body, notification_date FROM Notifications WHERE player_id = '$playerid' AND seen ='0'";
	$r = mysqli_query($dbc,$query);

	if(mysqli_num_rows($r) == 0){
		echo "You have no notifications";
	}
	else{
	//Showing notification
	if(isset($_GET['id']) AND isset($_GET['read'])){
		markAsRead($dbc, $_GET['id']);
	}
	elseif(isset($_GET['id'])){
		$query = "SELECT notification_id, title, body, notification_date FROM Notifications WHERE player_id = '$playerid' AND seen ='0' AND notification_id = '{$_GET['id']}'";
		$r = mysqli_query($dbc,$query);
		$data = mysqli_fetch_array($r, MYSQLI_ASSOC);
		echo "<h3>".$data['title']."</h3>";
		echo "<div class='well well-lg'>".$data['body']."</div>";
		echo "<a class='btn btn-primary' href='notifications.php'>Return</a>";
	}
	else{
	echo "<div class='col-lg-5'>
				<table class='table table-hover'>
				<tr><th>Title</th><th>Date</th><td></td><td></td></tr>";
	while($data = mysqli_fetch_array($r, MYSQLI_ASSOC)){
		?>
			<tr>
				<td><?php echo $data['title']; ?></td>
				<td><?php echo $data['notification_date']; ?></td>
				<td><?php echo "<a class='btn btn-primary' href='notifications.php?&id={$data['notification_id']}'>Read</a>"; ?></td>
				<td><?php echo "<a class='btn btn-primary' href='notifications.php?&id={$data['notification_id']}&read=1'>Mark as Read</a>"; ?></td>
			</tr>
		<?php
	}
	echo "</table>				
				  </div>";
	}
}
 ?>
</div>
 <?php include("footer.php"); ?>